﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Views_module_cart_home : System.Web.UI.UserControl
{
    public String url_checkout = "https://www.nganluong.vn/advance_payment.php";
    DataTable dt;
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack){
            LoadData();
        }
        //LoadButtonCheckout();
    }

    public void LoadData()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("image");
        dt.Columns.Add("product_id");
        dt.Columns.Add("product_name");
        dt.Columns.Add("price");
        dt.Columns.Add("quantity");
        dt.Columns.Add("total");
        DataRow dr;
        for (int i = 0; i < cartBus.carts.listcart.Count; i++)
        {
            product pro = cartBus.carts.listcart[i].Product;
            dr = dt.NewRow();
            int num;
            dr["image"] = "~//images//"+pro.Image;
            dr["product_id"] = pro.Product_id;
            dr["product_name"] = pro.Product_name;
            dr["price"] = pro.Price;
            dr["quantity"] = cartBus.carts.listcart[i].Quantity;
            dr["total"] = pro.Price * cartBus.carts.listcart[i].Quantity;
            dt.Rows.Add(dr);        
            
        }
        dtlCartProducts.DataSource = dt;
        dtlCartProducts.DataBind();
    }

    protected void dtlCartProducts_EditCommand(object source, DataListCommandEventArgs e)
    {
        
        dtlCartProducts.EditItemIndex = (int)e.Item.ItemIndex;
        LoadData();
    }
    protected void dtlCartProducts_DeleteCommand(object source, DataListCommandEventArgs e)
    {
        int product_id = Convert.ToInt32(dtlCartProducts.DataKeys[e.Item.ItemIndex]);
        cartBus.carts.RemoveItem(product_id);
        Response.Redirect("~//default.aspx?action=cart_home");
    }
    protected void dtlCartProducts_CancelCommand(object source, DataListCommandEventArgs e)
    {
        dtlCartProducts.EditItemIndex = -1;
        LoadData();
    }

    protected void dtlCartProducts_UpdateCommand(object source, DataListCommandEventArgs e)
    {
        int product_id = Convert.ToInt32(dtlCartProducts.DataKeys[e.Item.ItemIndex]);
        int q = Convert.ToInt32(((TextBox)e.Item.FindControl("quantity2")).Text.ToString());
        //ScriptManager.RegisterStartupScript(this, typeof(Page), "Test.com", "<script>alert('" + q + "')</script>", false);
        cartBus.carts.setQuantityProduct(product_id, q);
        dtlCartProducts.EditItemIndex = -1;
        Response.Redirect("default.aspx?action=cart_home");
    }

    protected void continue_Click(object sender, EventArgs e)
    {
        Response.Redirect("default.aspx");
    }

    protected void LoadButtonCheckout()
    {
        String receiver = "ngocnh210@gmail.com";
        String comments = "Thanh toán sản phẩm";
        String return_url = "http://localhost:1234/Sem3/default.aspx?action=checkout_thank";
        String price = cartBus.carts.subtotal().ToString();
        String product = "Hãy lập trình tên giỏ hàng của bạn vào đây";
        this.url_checkout += "?receiver=" + receiver + "&comments=" + comments + "&return_url=" + return_url + "&product=" + product + "&price=" + price;
        ImageButton BTN_Checkout = new ImageButton();
        BTN_Checkout.PostBackUrl = this.url_checkout;
        BTN_Checkout.ImageUrl = "https://www.nganluong.vn/data/images/buttons/3.gif";
        //btnCheckout.Controls.Add(BTN_Checkout);
    }
    protected void continue_checkout(object sender, EventArgs e)
    {
        if (Session["username"] == null || Session["username"]=="")
        {
            ScriptManager.RegisterStartupScript(this, typeof(Page), "Test.com", "<script>alert('Please Login to Continue')</script>", false);
            
        }else{
            Response.Redirect("~//order.aspx?step=1");
        }
        
    }
}